<?php
require_once 'config/database.php';
session_start();

// 检查是否是管理员
if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    echo json_encode(['success' => false, 'message' => '权限不足']);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $id = (int)$_POST['id'];
    $status = $conn->real_escape_string($_POST['status']);
    
    // 根据状态自动更新相应的日期
    $date_field = '';
    switch ($status) {
        case '预处理上线':
            $date_field = 'preprocess_online_date';
            break;
        case '预处理入库':
            $date_field = 'preprocess_storage_date';
            break;
        case '机芯下线':
            $date_field = 'core_offline_date';
            break;
        case '机芯转涂':
            $date_field = 'core_coating_date';
            break;
        case '机芯入库':
            $date_field = 'core_storage_date';
            break;
        case '壳体安装':
            $date_field = 'shell_install_date';
            break;
        case '整锤转涂':
            $date_field = 'hammer_coating_date';
            break;
        case '整锤入库':
            $date_field = 'hammer_storage_date';
            break;
        case '发车':
            $date_field = 'delivery_date';
            break;
    }

    $today = date('Y-m-d');
    $sql = "UPDATE production_details SET 
            production_status = '$status'";
    
    if ($date_field) {
        $sql .= ", $date_field = '$today'";
    }
    
    $sql .= " WHERE id = $id";
    
    if ($conn->query($sql)) {
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'message' => $conn->error]);
    }
} else {
    echo json_encode(['success' => false, 'message' => '无效的请求方法']);
} 